package com.example.directive;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.example.directive.layout.Mlayout;
import com.example.directive.layout.Tlayout;
import com.example.directive.order.cOrder;
import com.example.directive.order.order;
import com.example.directive.order.tOrder;
import com.example.directive.param.Param;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        String mJson = "[{\"type\":1,\"title\":\"2222\",\"placeholder\":\"\",\"example\":\"\",\"required\":false,\"order\":\"\",\"content\":\"33333\"},{\"type\":1,\"title\":\"4444\",\"placeholder\":\"\",\"example\":\"\",\"required\":false,\"order\":\"\",\"content\":\"5555\"}]";
        String tJson = "[{\"type\":0,\"title\":\"企业名称\",\"placeholder\":\"企业名称\",\"example\":\"深圳市腾讯计算机系统有限公司\",\"required\":true,\"order\":\"\",\"key\":\"88832fcb-1e1b-4467-800b-7b5d69f01fb1\",\"content\":\"22228888\"},{\"type\":0,\"title\":\"所属行业\",\"placeholder\":\"所属行业\",\"example\":\"互联网\",\"required\":true,\"order\":\"\",\"key\":\"c594ad6c-b165-4734-9442-2733f4f1d98c\",\"content\":\"4444\"},{\"type\":0,\"title\":\"主营业务\",\"placeholder\":\"主营业务\",\"example\":\"技术开发\",\"required\":true,\"order\":\"\",\"key\":\"9e529d44-9086-4506-ab8a-4f0285a15e16\",\"content\":\"444\"},{\"type\":0,\"title\":\"企业规模\",\"placeholder\":\"企业规模\",\"example\":\"7000-7999人\",\"required\":true,\"order\":\"\",\"key\":\"7ddb4bb1-6bf8-427f-bc92-5f1400b194cf\",\"content\":\"4444\"},{\"type\":0,\"title\":\"荣誉资质\",\"placeholder\":\"荣誉资质\",\"example\":\"业界Top1\",\"required\":false,\"order\":\"\",\"key\":\"1a1e8c2f-e85a-4f13-9bfe-0cb02e9ef583\",\"content\":\"4444\"},{\"type\":0,\"title\":\"联系方式\",\"placeholder\":\"联系方式\",\"example\":\"12345678910\",\"required\":false,\"order\":\"\",\"key\":\"cc70cd3a-3793-4b63-a5d6-ea4ecfc7ce4e\",\"content\":\"55555\"}]";

        String scene = "你现在是一个求职顾问，请根据我的情况，为我从以下三家公司中选择一家：\n" +
                "1.娃哈哈食品公司\n" +
                "2.联想公司\n" +
                "3.大明白服装厂\n" +
                "以下是我的具体情况：\n" +
                "{字段1指令}，{字段2指令}，{字段4指令}，{字段3指令}。\n" +
                "我所处的行业特点是：借助微信朋友圈等社交媒体进行商品营销。{行业属性}";

//            String orderJson = "{\"template\":[{\"type\":0,\"title\":\"姓名\",\"placeholder\":\"请输入您的姓名\",\"example\":\"张三\",\"required\":true,\"order\":\"我的名字叫{1}\"},{\"type\":1,\"title\":\"工作经历\",\"placeholder\":\"请输入您的工作经历\",\"example\":\"曾经在哈佛大学担当助教，回国后在华为公司担任技术总监\",\"required\":true,\"order\":\"以下是我的工作经历：{2}\"},{\"type\":1,\"title\":\"擅长领域\",\"placeholder\":\"请输入您的擅长领域\",\"example\":\"我擅长office软件的使用\",\"required\":false,\"order\":\"我特别擅长{3}\"},{\"type\":2,\"title\":\"我的技能证书\",\"placeholder\":\"请选择您的技能\",\"example\":\"\",\"required\":false,\"order\":\"我拥有{4}证书\",\"child\":[{\"value\":\"英语8级\",\"isCustom\":false},{\"value\":\"日文8级\",\"isCustom\":false},{\"value\":\"计算机二级\",\"isCustom\":false},{\"value\":\"\",\"isCustom\":true}]}]}";
        String orderJson = "\n" +
                "{\n" +
                "\n" +
                "    \"template\":[\n" +
                "        {\n" +
                "            \"type\":2,\n" +
                "            \"title\":\"产品名称\",\n" +
                "            \"placeholder\":\"\",\n" +
                "            \"example\":\"\",\n" +
                "            \"required\":true,\n" +
                "            \"order\":\"产品名称：{1}\",\n" +
                "            \"child\":[\n" +
                "                {\n" +
                "                    \"value\":\"泰康健康尊享\",\n" +
                "                    \"isCustom\":false\n" +
                "                }\n" +
                "            ]\n" +
                "        },\n" +
                "        {\n" +
                "            \"type\":3,\n" +
                "            \"title\":\"产品与服务\",\n" +
                "            \"placeholder\":\"提示提示\",\n" +
                "            \"required\":true,\n" +
                "            \"orders\":[\n" +
                "                {\n" +
                "                    \"key\":\"88832fcb-1e1b-4467-800b-7b5d69f01fb1\",\n" +
                "                    \"title\":\"产品名称\",\n" +
                "                    \"order\":\"产品的名称是{88832fcb-1e1b-4467-800b-7b5d69f01fb1}\"\n" +
                "                },\n" +
                "                {\n" +
                "                    \"key\":\"c594ad6c-b165-4734-9442-2733f4f1d98c\",\n" +
                "                    \"title\":\"产品名称\",\n" +
                "                    \"order\":\"他的价格是{c594ad6c-b165-4734-9442-2733f4f1d98c}\"\n" +
                "                }\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "\n" +
                "}\n";

        JSONArray mJsonArray = JSONUtil.parseArray(mJson);
        JSONArray tJsonArray = JSONUtil.parseArray(tJson);

        /*
        * 1.获取用户填写的自定义内容
        * 2.获取用户填写的模板内容
        * 3.获取指令
        * 4.拼接指令
        * 4.1小指令
        * 4.2最终指令
        * */

        //用户填写的自定义内容
        StringBuilder mstr = new StringBuilder();
        for (Object obj : mJsonArray) {
            Mlayout mlayout = JSONUtil.toBean(obj.toString(), Mlayout.class);
            String str = mlayout.getTitle() + ":" + mlayout.getContent() + ";";
            mstr.append(str);
        }
        System.out.println(mstr);

        //用户填写的模板内容
        Map<String, String> tLayoutParams = new HashMap<>();
        for (Object obj : tJsonArray) {
            Tlayout tlayout = JSONUtil.toBean(obj.toString(), Tlayout.class);
            tLayoutParams.put(tlayout.getKey(), tlayout.getContent());
        }

        //获取指令
        tOrder tOrder = JSONUtil.toBean(orderJson, com.example.directive.order.tOrder.class);
        List<order> orderList = tOrder.getTemplate();
        System.out.println(orderList);

        /*
        * 拼接小指令
        * 1.分两类
        * 1.1语料库
        * 1.2普通小指令
        * 2.组装成Map
        * */

        //参数处理
        //1 : {type:1, content: "xxx"}
        Map<String, String> mapTest = new HashMap<>();
        mapTest.put("2", "{type:3, content: \"xxx\"}");


        Map<String, String> lastMap = new HashMap<>();
        for (int i = 0; i < orderList.size(); i++) {
            String str = mapTest.get(String.valueOf(i+1));
            Param p = JSONUtil.toBean(str, Param.class);
            System.out.println("第" + (i+1) + "次");
            System.out.println(p);
            if (ObjectUtil.isNotEmpty(p.getType()) && p.getType() == 3L) {
                if (ObjectUtil.isEmpty(p.getContent())) {
                    lastMap.put("字段" + (i+1) + "指令", "");
                } else {
                    //通过content获取语料库数据
                    //组装指令
                    List<cOrder> cOrderList = orderList.get(i).getOrders();
                    StringBuilder cOrderStr = new StringBuilder();
                    for (cOrder cOrder : cOrderList) {
                        cOrderStr.insert(0, StrUtil.format(cOrder.getOrder(), tLayoutParams));
                    }
                    cOrderStr.append(mstr);
                    lastMap.put("字段" + (i+1) + "指令", cOrderStr.toString());
                }
            } else {
                order order = orderList.get(i);
                if (ObjectUtil.isNotEmpty(p.getContent()) && ObjectUtil.isNotEmpty(order.getOrder())) {
                    Map<String, String> map = new HashMap<>();
                    map.put(String.valueOf(i+1), p.getContent());
                    lastMap.put("字段" + (i+1) + "指令", StrUtil.format(order.getOrder(), map));
                } else if ((ObjectUtil.isEmpty(p.getContent()) && ObjectUtil.isNotEmpty(order.getOrder()))
                        || ObjectUtil.isEmpty(order.getOrder())) {
                    lastMap.put("字段" + (i+1) + "指令", "");
                } else {
                    //直接组装
                    System.out.println(StrUtil.format(scene, mapTest));
                }
            }
        }
        System.out.println("---------------------------------");
        System.out.println(StrUtil.format(scene, lastMap));
    }
}
